var login = {};
login.confirm = function () {
    if (sessionStorage.user) {
        document.getElementById('login').style.display = "none";
        user = JSON.parse(sessionStorage.user);
        login.formatLoginImg(user.id), login.connection(user)
    } else {
        return document.getElementById('user').style.display = "none"
    }
};

login.formatLoginImg = function (id) {
    var idText = document.getElementById("user").getElementsByTagName("a")[0];
    var idStr = idText.innerHTML.split(">");
    return idText.innerHTML = idStr[0] + ">" + id;
};

login.quit = function () {
    sessionStorage.removeItem('user'), login.formatLoginImg(""), websockets[0].close(), websockets = [];
    user.id = "", user.password = "";
    document.getElementById('login').style.display = "inline-block", document.getElementById('user').style.display = "none";
};

login.connection = function (user) {
    var wsUrl = "ws://192.168.80.66:9321";
    initWs(wsUrl, user);
};

var operate = {};
operate.prototype = {
    sendMsg: function () {
        if (user.id === "") return layui.use('layer', function () {
            var layer = layui.layer;
            layer.alert("你还没有登录", {
                icon: 5, yes: function (index, layero) {
                    layer.close(index), register.open();
                }
            })
        });

        var textele = document.getElementById('text');
        if (textele.value.replace(/(^\s*)|(\s*$)/g, "").length === 0) return;
        var command = Command.values.COMMAND_CHAT_REQ;
        var bodyData = {
            text: textele.value,
            group: 'g',
            type: ChatType.values.CHAT_TYPE_PUBLIC

        };
        sendPacket(websockets[0], command, ChatReqBody, bodyData);
        textele.value = "";
    }, loadUsers: function (user) {
        if (typeof user === "undefined" || user === null) return;

        for (var i = 0, l = user.length; i < l; i++) {
            if (typeof user[i] === "undefined") continue;
            users.chatRoomMembers.push(user[i]);
        }
    }
};


var register = {};
register.open = function () {
    layui.use('layer', function () {
        var layer = layui.layer;

        layer.open({
            type: 1,
            title: "用户注册",
            area: ['400px', '260px'],
            content: "<ul class='user-register'><form class='layui-form' action=''><div class='layui-form-item'>" +
            "<label class='layui-form-label'>输入框</label>" +
            "<div class='layui-input-block'>" +
            "<input id='userid' type='text' name='title' required  lay-verify='required' placeholder='请输入标题' autocomplete='off' class='layui-input'>" +
            "</div>" +
            "</div>" +
            "<div class='layui-form-item'>" +
            "<label class='layui-form-label'>密码框</label>" +
            "<div class='layui-input-block'>" +
            "<input id='userpwd' type='password' name='password' required lay-verify='required' placeholder='请输入密码' autocomplete='off' class='layui-input'>" +
            "</div>" +
            // "<div class='layui-form-mid layui-word-aux'>辅助文字</div>" +
            "</div></form></ul>",
            btn: ["注册", "放弃"],
            btnAlign: "c",
            yes: function (index, layero) {
                user.id = document.getElementById('userid').value, user.password = document.getElementById('userpwd').value;
                if (user.id === "" || user.password === "") return layer.tips('用户名或密码不能为空', "#userid"), user.id = "", user.password = "", null;
                return layer.close(index),
                    document.getElementById('user').style.display = "inline-block",
                    document.getElementById('login').style.display = "none",
                    sessionStorage.user = JSON.stringify(user), layer.msg('注册成功', {
                    anim: 0,
                    icon: 3,
                    shade: 0,
                    time: 2000
                }), login.connection(user), login.formatLoginImg(user.id);
            }, btn2: function (index, layero) {
                return layer.close(index), user.id = "", user.password = "", null;
            }
        })
    })
};

document.getElementById('text').onkeypress = function (e) {
    if (e.which == 13) {
        operate.prototype.sendMsg();
    }
};



